<?php

namespace App\Console\Commands\OpenShop;

use App\Models\Ekb\EkbApplyHistoryModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use App\Remote\EKuaiBao\EKuaiBaoRemote;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class EkbPollingApproveStatesCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ekb:pollingApproveStates';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '易快报 - 轮询退款单结果';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     * @throws GuzzleException
     */
    public function handle(): int
    {
        EkbApplyHistoryModel::query()->where("status", 0)->chunkById(100, function (Collection $collection) {
            $collection->each(function (EkbApplyHistoryModel $ekbApplyHistoryModel) {
                try {
                    $flowDetails                         = EKuaiBaoRemote::flowDetails(
                        $ekbApplyHistoryModel->flowId,
                        true
                    );
                    $ekbApplyHistoryModel->approveResult = json_encode($flowDetails, JSON_UNESCAPED_UNICODE);
                    if (
                        !empty($flowDetails["value"]["state"])
                        &&
                        in_array($flowDetails["value"]["state"], ["archived", "sending", "receiving", "paid"])) {
                        $ekbApplyHistoryModel->status = 1;
                        //更改装修保证金状态
                        $openShopUserContractModel                          = OpenShopUserContractModel::query()
                                                                                                       ->where("orderNo",
                                                                                                           $ekbApplyHistoryModel->orderNo)
                                                                                                       ->first();
                        $openShopUserContractModel->refundDecorationDeposit = $openShopUserContractModel->refundDecorationDeposit + $ekbApplyHistoryModel->refundAmount;
                        $openShopUserContractModel->save();
                    }
                    $ekbApplyHistoryModel->save();
                } catch (\Exception $exception) {
                    if ($exception->getMessage() == "单据已删除") {
                        $ekbApplyHistoryModel->isDeleted = 1;
                        $ekbApplyHistoryModel->save();
                    } else {
                        logException(
                            "EKB 轮询获取退款单审批结果数据失败",
                            $exception,
                            ["orderNo" => $ekbApplyHistoryModel->orderNo],
                            true,
                            true
                        );
                    }
                }
            });
        });
        return 0;
    }
}
